package com.woniuxy.threeproject.end_water_project_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.threeproject.end_water_project_user.domain.Account;
import com.woniuxy.threeproject.end_water_project_user.dao.AccountMapper;
import com.woniuxy.threeproject.end_water_project_user.exception.EndWaterRuntimeException;
import com.woniuxy.threeproject.end_water_project_user.service.IAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 账号表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@Service
@Slf4j
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {

    @Resource
    public AccountMapper accountMapper;
    @Override
    public int login(Account ac) {
        log.info("开始登录用户:{}",ac);
        QueryWrapper<Account> qw = new QueryWrapper<>();
        qw.eq("account",ac.getAccount());
        Account account = accountMapper.selectOne(qw);
        if(null==account){
//            表示没有查询到该用户
            return 0;
        }else{
//            返回的数据和用户传来的数据作对比
            if(ac.getPassword().equals(account.getPassword())){
                return 1;
            }else{
//                表示密码不正确
                return 2;
            }
        }
    }

    @Override
    public Account addAccount(Account ac) {
        log.info("serviceImp开始新增账户表！");
        int i = accountMapper.insert(ac);
        if(i>0){
            return ac;
        }else{
            throw  new EndWaterRuntimeException("账户加入数据库失败！");
        }

    }

    @Override
    public Account selectByAccount(Long name) {
        QueryWrapper<Account> qw = new QueryWrapper<>();
        qw.eq("account",name);
        Account ac = accountMapper.selectOne(qw);
        return ac;
    }
}
